home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
oath.lha
/
oath
/
test
/
timer.h
< prev
next >
Wrap
C/C++ Source or Header
|
1991-08-29
|
7KB
|
240 lines
#ifndef TIMER_H
#define TIMER_H
//***************************************************************************
// TIMER :: A class for software performance analysis
//***************************************************************************
//
// Copyright (C) 1990, 1989 Texas Instruments Incorporated
// Permission is granted to any individual or institution
// to use, copy, modify, and distribute this software,
// provided that this complete copyright and permission notice
// is maintained, intact, in all copies and supporting documentation.
//
// Texas Instruments Incorporated provides this software "as is"
// without express or implied warranty.
//
//***************************************************************************
//
// History:
// 06/90 Brian M Kennedy New naming convention
// 07/89 Brian M Kennedy Rewritten in C++ (CADET)
// 12/88 Brian M Kennedy Added ability to have multiple timers (PPODEM)
// Joseph T Rahmeh Original in C (UT Austin)
//
//***************************************************************************
//
// This file provides timing code for performance evaluation of software.
// The timer class operates much like a stopwatch with a START/STOP button
// and a SPLIT button. The start() operation resets the stopwatch to zero.
// The split() operation captures the amount of time since the last start().
// Thus, you can capture the time repeatedly by doing multiple split() cals
// after a single start() call.
//
// Functions user(), system(), cpu(), and real() read the times captured by
// split(), where
//
// User time:
// time CPU spends in user mode on behalf of the program.
// System time:
// time CPU spends in system mode on behalf of the program.
// All time:
// total CPU time.
// Real time:
// what you get from a stop watch timer (or the clock on the wall).
//
// Functions which have the suffix "US" return time values in microseconds.
// All other functions return time values in milliseconds.
//
// This code was adapted for C++ from C code written by Brian Kennedy
// for a project performed at UT Austin. That code was derived from
// code originally written by Dr. Joseph Rahmeh at UT Austin.
//
//***************************************************************************
#include <sys/time.h>
#include <sys/resource.h>
extern int getrusage (int, rusage *);
#include <sys/types.h>
#include <sys/timeb.h>
extern int ftime (timeb *);
/***************************************************************************/
// The TIMER Class Definition
class timer
{public:
inline timer(); // Constructs timer and does start()
inline void mark(); // Obsolete synonym for start()
// Timer operation
inline void start(); // Reset timer to zero.
inline void split(); // Capture time since last start()
// Read times from last start() to last split()
inline long real() const; // real time (ms)
inline long user() const; // user time (ms)
inline long system() const; // system time (ms)
inline long cpu() const; // user+system time (ms)
inline long userUS() const; // user time (us)
inline long systemUS() const; // system time (us)
inline long cpuUS() const; // user+system time (us)
// Times for this process up to last split()
inline long totalUser() const; // user time (ms)
inline long totalSystem() const;// system time (ms)
inline long totalCpu() const; // user+system time (ms)
private:
rusage Usage; // current rusage structure
rusage Usage0; // rusage structure at last mark
timeb Real; // current elapsed real time
timeb Real0; // elapsed real time at last mark
}; // timer
/***************************************************************************/
// Inline Member Functions
inline void
timer::mark() // obsolete
{start();}
// Manipulators
inline void
timer::start()
{getrusage(0, &Usage0);
ftime(&Real0);
}
inline void
timer::split()
{getrusage(0, &Usage);
ftime(&Real);
}
// Constructor
inline
timer::timer()
{start();
split();
}
// Accessors
inline long
timer::real() const
{long s = Real.time - Real0.time;
long ms = Real.millitm - Real0.millitm;
if(ms < 0)
{ms += 1000;
s--;
}
return 1000*s + ms;
}
inline long
timer::user() const
{long dsec = Usage.ru_utime.tv_sec - Usage0.ru_utime.tv_sec;
long dusec = Usage.ru_utime.tv_usec - Usage0.ru_utime.tv_usec;
if(dusec < 0)
{dusec += 1000000;
dsec--;
}
return(dsec*1000 + dusec/1000);
}
inline long
timer::system() const
{long dsec = Usage.ru_stime.tv_sec - Usage0.ru_stime.tv_sec;
long dusec = Usage.ru_stime.tv_usec - Usage0.ru_stime.tv_usec;
if(dusec < 0)
{dusec += 1000000;
dsec--;
}
return(dsec*1000 + dusec/1000);
}
inline long
timer::cpu() const
{long dsec = Usage.ru_utime.tv_sec + Usage.ru_stime.tv_sec
- Usage0.ru_utime.tv_sec - Usage0.ru_stime.tv_sec;
long dusec = Usage.ru_utime.tv_usec + Usage.ru_stime.tv_usec
- Usage0.ru_utime.tv_usec - Usage0.ru_stime.tv_usec;
if(dusec < 0)
{dusec += 1000000;
dsec--;
}
return(dsec*1000 + dusec/1000);
}
inline long
timer::userUS() const
{long dsec = Usage.ru_utime.tv_sec - Usage0.ru_utime.tv_sec;
long dusec = Usage.ru_utime.tv_usec - Usage0.ru_utime.tv_usec;
if(dusec < 0)
{dusec += 1000000;
dsec--;
}
return(dsec*1000000 + dusec);
}
inline long
timer::systemUS() const
{long dsec = Usage.ru_stime.tv_sec - Usage0.ru_stime.tv_sec;
long dusec = Usage.ru_stime.tv_usec - Usage0.ru_stime.tv_usec;
if(dusec < 0)
{dusec += 1000000;
dsec--;
}
return(dsec*1000000 + dusec);
}
inline long
timer::cpuUS() const
{long dsec = Usage.ru_utime.tv_sec + Usage.ru_stime.tv_sec
- Usage0.ru_utime.tv_sec - Usage0.ru_stime.tv_sec;
long dusec = Usage.ru_utime.tv_usec + Usage.ru_stime.tv_usec
- Usage0.ru_utime.tv_usec - Usage0.ru_stime.tv_usec;
if(dusec < 0)
{dusec += 1000000;
dsec--;
}
return(dsec*1000000 + dusec);
}
inline long
timer::totalUser() const
{return( Usage.ru_utime.tv_sec * 1000
+ Usage.ru_utime.tv_usec / 1000);
}
inline long
timer::totalSystem() const
{return( Usage.ru_stime.tv_sec * 1000
+ Usage.ru_stime.tv_usec / 1000);
}
inline long
timer::totalCpu() const
{return( (Usage.ru_utime.tv_sec + Usage.ru_stime.tv_sec) *1000
+ (Usage.ru_utime.tv_usec + Usage.ru_stime.tv_usec)/1000);
}
/***************************************************************************/
#endif // TIMER_H